//#include "stdafx.h"
#include <common.h>
#include <XUtil/XDateTime.hpp>
#include <boost/property_tree/ptree.hpp>  
#include <boost/property_tree/json_parser.hpp>  
#include <boost/property_tree/xml_parser.hpp>  
#include <boost/typeof/typeof.hpp>  
#include <boost/foreach.hpp>  
#include <boost/locale.hpp>
#include <utility>

#ifdef USE_LOGXXX

static LogMgr* g_log = nullptr;

void LogMgr::Init()
{
	new LogMgr();
}

void LogMgr::Term()
{
	if (g_log) {
		delete g_log;
	}
}

void LogMgr::LogOut(const char *fmt, ...)
{
	if (g_log) {
		std::string buf(2048,0);
		va_list args;
		va_start(args, fmt);
		buf.resize(vsnprintf((char*)buf.data(), buf.size(), fmt, args));
		va_end(args);
		g_log->DoLogOut(std::move(buf));
	}
}

LogMgr::LogMgr()
{
	g_log = this;
	thread_ = std::make_shared<std::thread>([this] {
		do {
			std::unique_lock<std::mutex> lock(mutex_);
			cv_.wait(lock);
			DoFlush();
		} while (g_log);
	});
}

LogMgr::~LogMgr()
{
	g_log = nullptr;
	if (thread_) {
		//std::unique_lock<std::mutex> lock(mutex_);
		cv_.notify_all();
		thread_->join();
		thread_.reset();
	}
}

void LogMgr::DoFlush()
{
	try {
		//std::unique_lock<std::mutex> lock(mutex_);
		char filename[256] = { 0 };
		time_t tt;
		time(&tt);
		strftime(filename, 256, STR_MODULE_NAME "_%Y-%m-%d.log", localtime(&tt));
		std::ofstream outfile(filename, std::ios::out | std::ios::app);
		//outfile.imbue(std::locale("zh_CN"));
		while (!records_.empty()) {
			auto record = records_.front();
			records_.pop();
			try {
				outfile << record;// << std::endl;
			}
			catch (...) {

			}
		}
	}
	catch (...) {

	}
}

void LogMgr::DoLogOut(std::string&& str)
{
	std::unique_lock<std::mutex> lock(mutex_);
	records_.emplace(std::move(str));
	cv_.notify_one();
}

#endif//USE_LOGXXX

HZQDB g_ctp_module = nullptr;
tagModuleInfoEx* g_module_info = nullptr;

const char* all_exchange[] = {
	EXCHANGE_CFFEX,
	EXCHANGE_SHFE,
	EXCHANGE_CZCE,
	EXCHANGE_DCE,
	EXCHANGE_INE,
	EXCHANGE_GFEX,
};

const size_t MaxProductCountPerExchange = 100;
const size_t MaxCodeCountPerProduct = 100;

//CODE
DEFINE_MDB_FIELD_COUNT(CTP, CODE);
DEFINE_MDB_FIELD(CTP, CODE, EXCHANGE, MDB_FIELD_TYPE_STRING, MAX_EXCHANGE_LENGTH + 1);
DEFINE_MDB_FIELD(CTP, CODE, PRODUCT, MDB_FIELD_TYPE_STRING, MAX_PRODUCT_LENGTH + 1);
DEFINE_MDB_FIELD(CTP, CODE, CODE, MDB_FIELD_TYPE_STRING, MAX_CODE_LENGTH + 1);
DEFINE_MDB_FIELD(CTP, CODE, NAME, MDB_FIELD_TYPE_STRING, MAX_NAME_LENGTH + 1);
DEFINE_MDB_FIELD(CTP, CODE, TRADE_CODE, MDB_FIELD_TYPE_STRING, MAX_CODE_LENGTH + 1);
DEFINE_MDB_FIELD(CTP, CODE, DATE, MDB_FIELD_TYPE_UINT32, sizeof(uint32_t));
DEFINE_MDB_FIELD(CTP, CODE, TIME, MDB_FIELD_TYPE_UINT32, sizeof(uint32_t));
DEFINE_MDB_FIELD(CTP, CODE, STATUS, MDB_FIELD_TYPE_CHAR, sizeof(char));
DEFINE_MDB_FIELD(CTP, CODE, ENTER_REASON, MDB_FIELD_TYPE_CHAR, sizeof(char));
DEFINE_MDB_FIELD(CTP, CODE, PRICETICK, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
DEFINE_MDB_FIELD(CTP, CODE, YCLOSE, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
DEFINE_MDB_FIELD(CTP, CODE, YSETTLE, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
DEFINE_MDB_FIELD(CTP, CODE, SETTLE, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
DEFINE_MDB_FIELD(CTP, CODE, UPPER, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
DEFINE_MDB_FIELD(CTP, CODE, LOWER, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
DEFINE_MDB_FIELD(CTP, CODE, OPEN, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
DEFINE_MDB_FIELD(CTP, CODE, HIGH, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
DEFINE_MDB_FIELD(CTP, CODE, LOW, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
DEFINE_MDB_FIELD(CTP, CODE, CLOSE, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
DEFINE_MDB_FIELD(CTP, CODE, AMOUNT, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
DEFINE_MDB_FIELD(CTP, CODE, VOLUME, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
DEFINE_MDB_FIELD(CTP, CODE, BID1, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
DEFINE_MDB_FIELD(CTP, CODE, BID2, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
DEFINE_MDB_FIELD(CTP, CODE, BID3, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
DEFINE_MDB_FIELD(CTP, CODE, BID4, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
DEFINE_MDB_FIELD(CTP, CODE, BID5, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
DEFINE_MDB_FIELD(CTP, CODE, BIDV1, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
DEFINE_MDB_FIELD(CTP, CODE, BIDV2, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
DEFINE_MDB_FIELD(CTP, CODE, BIDV3, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
DEFINE_MDB_FIELD(CTP, CODE, BIDV4, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
DEFINE_MDB_FIELD(CTP, CODE, BIDV5, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
DEFINE_MDB_FIELD(CTP, CODE, ASK1, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
DEFINE_MDB_FIELD(CTP, CODE, ASK2, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
DEFINE_MDB_FIELD(CTP, CODE, ASK3, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
DEFINE_MDB_FIELD(CTP, CODE, ASK4, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
DEFINE_MDB_FIELD(CTP, CODE, ASK5, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
DEFINE_MDB_FIELD(CTP, CODE, ASKV1, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
DEFINE_MDB_FIELD(CTP, CODE, ASKV2, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
DEFINE_MDB_FIELD(CTP, CODE, ASKV3, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
DEFINE_MDB_FIELD(CTP, CODE, ASKV4, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
DEFINE_MDB_FIELD(CTP, CODE, ASKV5, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
//
DEFINE_MDB_FIELD(CTP, CODE, PreOpenInterest, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
DEFINE_MDB_FIELD(CTP, CODE, OpenInterest, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
//
DEFINE_MDB_FIELD(CTP, CODE, PreDelta, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
DEFINE_MDB_FIELD(CTP, CODE, CurrDelta, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
//
DEFINE_MDB_FIELD(CTP, CODE, DeliveryYear, MDB_FIELD_TYPE_UINT32, sizeof(uint32_t));
DEFINE_MDB_FIELD(CTP, CODE, DeliveryMonth, MDB_FIELD_TYPE_UINT32, sizeof(uint32_t));
DEFINE_MDB_FIELD(CTP, CODE, MaxMarketOrderVolume, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
DEFINE_MDB_FIELD(CTP, CODE, MinMarketOrderVolume, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
DEFINE_MDB_FIELD(CTP, CODE, MaxLimitOrderVolume, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
DEFINE_MDB_FIELD(CTP, CODE, MinLimitOrderVolume, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
DEFINE_MDB_FIELD(CTP, CODE, VolumeMultiple, MDB_FIELD_TYPE_UINT32, sizeof(uint32_t));
DEFINE_MDB_FIELD(CTP, CODE, CreateDate, MDB_FIELD_TYPE_UINT32, sizeof(uint32_t));
DEFINE_MDB_FIELD(CTP, CODE, OpenDate, MDB_FIELD_TYPE_UINT32, sizeof(uint32_t));
DEFINE_MDB_FIELD(CTP, CODE, ExpireDate, MDB_FIELD_TYPE_UINT32, sizeof(uint32_t));
DEFINE_MDB_FIELD(CTP, CODE, StartDelivDate, MDB_FIELD_TYPE_UINT32, sizeof(uint32_t));
DEFINE_MDB_FIELD(CTP, CODE, EndDelivDate, MDB_FIELD_TYPE_UINT32, sizeof(uint32_t));
DEFINE_MDB_FIELD(CTP, CODE, InstLifePhase, MDB_FIELD_TYPE_CHAR, sizeof(char));
DEFINE_MDB_FIELD(CTP, CODE, IsTrading, MDB_FIELD_TYPE_BYTE, sizeof(byte));
DEFINE_MDB_FIELD(CTP, CODE, PositionType, MDB_FIELD_TYPE_CHAR, sizeof(char));
DEFINE_MDB_FIELD(CTP, CODE, PositionDateType, MDB_FIELD_TYPE_CHAR, sizeof(char));
DEFINE_MDB_FIELD(CTP, CODE, LongMarginRatio, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
DEFINE_MDB_FIELD(CTP, CODE, ShortMarginRatio, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
DEFINE_MDB_FIELD(CTP, CODE, MaxMarginSideAlgorithm, MDB_FIELD_TYPE_CHAR, sizeof(char));
DEFINE_MDB_FIELD(CTP, CODE, UnderlyingInstrID, MDB_FIELD_TYPE_STRING, 31);
DEFINE_MDB_FIELD(CTP, CODE, StrikePrice, MDB_FIELD_TYPE_CHAR, sizeof(char));
DEFINE_MDB_FIELD(CTP, CODE, OptionsType, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
DEFINE_MDB_FIELD(CTP, CODE, UnderlyingMultiple, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
DEFINE_MDB_FIELD(CTP, CODE, CombinationType, MDB_FIELD_TYPE_CHAR, sizeof(char));
BEGIN_MDB_FIELD_TABLE(CTP, CODE)
	DEFINE_MDB_FIELD_TABLE_FIELD(CTP, CODE, EXCHANGE)
	DEFINE_MDB_FIELD_TABLE_FIELD(CTP, CODE, PRODUCT)
	DEFINE_MDB_FIELD_TABLE_FIELD(CTP, CODE, CODE)
	DEFINE_MDB_FIELD_TABLE_FIELD(CTP, CODE, NAME)
	DEFINE_MDB_FIELD_TABLE_FIELD(CTP, CODE, TRADE_CODE)
	DEFINE_MDB_FIELD_TABLE_FIELD(CTP, CODE, DATE)
	DEFINE_MDB_FIELD_TABLE_FIELD(CTP, CODE, TIME)
	DEFINE_MDB_FIELD_TABLE_FIELD(CTP, CODE, STATUS)
	DEFINE_MDB_FIELD_TABLE_FIELD(CTP, CODE, ENTER_REASON)
	DEFINE_MDB_FIELD_TABLE_FIELD(CTP, CODE, PRICETICK)
	DEFINE_MDB_FIELD_TABLE_FIELD(CTP, CODE, YCLOSE)
	DEFINE_MDB_FIELD_TABLE_FIELD(CTP, CODE, YSETTLE)
	DEFINE_MDB_FIELD_TABLE_FIELD(CTP, CODE, SETTLE)
	DEFINE_MDB_FIELD_TABLE_FIELD(CTP, CODE, UPPER)
	DEFINE_MDB_FIELD_TABLE_FIELD(CTP, CODE, LOWER)
	DEFINE_MDB_FIELD_TABLE_FIELD(CTP, CODE, OPEN)
	DEFINE_MDB_FIELD_TABLE_FIELD(CTP, CODE, HIGH)
	DEFINE_MDB_FIELD_TABLE_FIELD(CTP, CODE, LOW)
	DEFINE_MDB_FIELD_TABLE_FIELD(CTP, CODE, CLOSE)
	DEFINE_MDB_FIELD_TABLE_FIELD(CTP, CODE, AMOUNT)
	DEFINE_MDB_FIELD_TABLE_FIELD(CTP, CODE, VOLUME)
	DEFINE_MDB_FIELD_TABLE_FIELD(CTP, CODE, BID1)
	DEFINE_MDB_FIELD_TABLE_FIELD(CTP, CODE, BID2)
	DEFINE_MDB_FIELD_TABLE_FIELD(CTP, CODE, BID3)
	DEFINE_MDB_FIELD_TABLE_FIELD(CTP, CODE, BID4)
	DEFINE_MDB_FIELD_TABLE_FIELD(CTP, CODE, BID5)
	DEFINE_MDB_FIELD_TABLE_FIELD(CTP, CODE, BIDV1)
	DEFINE_MDB_FIELD_TABLE_FIELD(CTP, CODE, BIDV2)
	DEFINE_MDB_FIELD_TABLE_FIELD(CTP, CODE, BIDV3)
	DEFINE_MDB_FIELD_TABLE_FIELD(CTP, CODE, BIDV4)
	DEFINE_MDB_FIELD_TABLE_FIELD(CTP, CODE, BIDV5)
	DEFINE_MDB_FIELD_TABLE_FIELD(CTP, CODE, ASK1)
	DEFINE_MDB_FIELD_TABLE_FIELD(CTP, CODE, ASK2)
	DEFINE_MDB_FIELD_TABLE_FIELD(CTP, CODE, ASK3)
	DEFINE_MDB_FIELD_TABLE_FIELD(CTP, CODE, ASK4)
	DEFINE_MDB_FIELD_TABLE_FIELD(CTP, CODE, ASK5)
	DEFINE_MDB_FIELD_TABLE_FIELD(CTP, CODE, ASKV1)
	DEFINE_MDB_FIELD_TABLE_FIELD(CTP, CODE, ASKV2)
	DEFINE_MDB_FIELD_TABLE_FIELD(CTP, CODE, ASKV3)
	DEFINE_MDB_FIELD_TABLE_FIELD(CTP, CODE, ASKV4)
	DEFINE_MDB_FIELD_TABLE_FIELD(CTP, CODE, ASKV5)
	//
	DEFINE_MDB_FIELD_TABLE_FIELD(CTP, CODE, PreOpenInterest)
	DEFINE_MDB_FIELD_TABLE_FIELD(CTP, CODE, OpenInterest)
	//
	DEFINE_MDB_FIELD_TABLE_FIELD(CTP, CODE, PreDelta)
	DEFINE_MDB_FIELD_TABLE_FIELD(CTP, CODE, CurrDelta)
	//
	DEFINE_MDB_FIELD_TABLE_FIELD(CTP, CODE, DeliveryYear)
	DEFINE_MDB_FIELD_TABLE_FIELD(CTP, CODE, DeliveryMonth)
	DEFINE_MDB_FIELD_TABLE_FIELD(CTP, CODE, MaxMarketOrderVolume)
	DEFINE_MDB_FIELD_TABLE_FIELD(CTP, CODE, MinMarketOrderVolume)
	DEFINE_MDB_FIELD_TABLE_FIELD(CTP, CODE, MaxLimitOrderVolume)
	DEFINE_MDB_FIELD_TABLE_FIELD(CTP, CODE, MinLimitOrderVolume)
	DEFINE_MDB_FIELD_TABLE_FIELD(CTP, CODE, VolumeMultiple)
	DEFINE_MDB_FIELD_TABLE_FIELD(CTP, CODE, CreateDate)
	DEFINE_MDB_FIELD_TABLE_FIELD(CTP, CODE, OpenDate)
	DEFINE_MDB_FIELD_TABLE_FIELD(CTP, CODE, ExpireDate)
	DEFINE_MDB_FIELD_TABLE_FIELD(CTP, CODE, StartDelivDate)
	DEFINE_MDB_FIELD_TABLE_FIELD(CTP, CODE, EndDelivDate)
	DEFINE_MDB_FIELD_TABLE_FIELD(CTP, CODE, InstLifePhase)
	DEFINE_MDB_FIELD_TABLE_FIELD(CTP, CODE, IsTrading)
	DEFINE_MDB_FIELD_TABLE_FIELD(CTP, CODE, PositionType)
	DEFINE_MDB_FIELD_TABLE_FIELD(CTP, CODE, PositionDateType)
	DEFINE_MDB_FIELD_TABLE_FIELD(CTP, CODE, LongMarginRatio)
	DEFINE_MDB_FIELD_TABLE_FIELD(CTP, CODE, ShortMarginRatio)
	DEFINE_MDB_FIELD_TABLE_FIELD(CTP, CODE, MaxMarginSideAlgorithm)
	DEFINE_MDB_FIELD_TABLE_FIELD(CTP, CODE, UnderlyingInstrID)
	DEFINE_MDB_FIELD_TABLE_FIELD(CTP, CODE, StrikePrice)
	DEFINE_MDB_FIELD_TABLE_FIELD(CTP, CODE, OptionsType)
	DEFINE_MDB_FIELD_TABLE_FIELD(CTP, CODE, UnderlyingMultiple)
	DEFINE_MDB_FIELD_TABLE_FIELD(CTP, CODE, CombinationType)
END_MDB_FIELD_TABLE()

BEGIN_MDB_FIELD_NAME_TABLE(CTP, CODE)
	DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, CODE, EXCHANGE)
	DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, CODE, PRODUCT)
	DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, CODE, CODE)
	DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, CODE, NAME)
	DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, CODE, TRADE_CODE)
	DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, CODE, DATE)
	DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, CODE, TIME)
	DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, CODE, STATUS)
	DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, CODE, ENTER_REASON)
	DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, CODE, PRICETICK)
	DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, CODE, YCLOSE)
	DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, CODE, YSETTLE)
	DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, CODE, SETTLE)
	DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, CODE, UPPER)
	DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, CODE, LOWER)
	DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, CODE, OPEN)
	DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, CODE, HIGH)
	DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, CODE, LOW)
	DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, CODE, CLOSE)
	DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, CODE, AMOUNT)
	DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, CODE, VOLUME)
	DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, CODE, BID1)
	DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, CODE, BID2)
	DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, CODE, BID3)
	DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, CODE, BID4)
	DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, CODE, BID5)
	DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, CODE, BIDV1)
	DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, CODE, BIDV2)
	DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, CODE, BIDV3)
	DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, CODE, BIDV4)
	DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, CODE, BIDV5)
	DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, CODE, ASK1)
	DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, CODE, ASK2)
	DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, CODE, ASK3)
	DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, CODE, ASK4)
	DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, CODE, ASK5)
	DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, CODE, ASKV1)
	DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, CODE, ASKV2)
	DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, CODE, ASKV3)
	DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, CODE, ASKV4)
	DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, CODE, ASKV5)
	//
	DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, CODE, PreOpenInterest)
	DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, CODE, OpenInterest)
	//
	DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, CODE, PreDelta)
	DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, CODE, CurrDelta)
	//
	DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, CODE, DeliveryYear)
	DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, CODE, DeliveryMonth)
	DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, CODE, MaxMarketOrderVolume)
	DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, CODE, MinMarketOrderVolume)
	DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, CODE, MaxLimitOrderVolume)
	DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, CODE, MinLimitOrderVolume)
	DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, CODE, VolumeMultiple)
	DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, CODE, CreateDate)
	DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, CODE, OpenDate)
	DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, CODE, ExpireDate)
	DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, CODE, StartDelivDate)
	DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, CODE, EndDelivDate)
	DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, CODE, InstLifePhase)
	DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, CODE, IsTrading)
	DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, CODE, PositionType)
	DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, CODE, PositionDateType)
	DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, CODE, LongMarginRatio)
	DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, CODE, ShortMarginRatio)
	DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, CODE, MaxMarginSideAlgorithm)
	DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, CODE, UnderlyingInstrID)
	DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, CODE, StrikePrice)
	DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, CODE, OptionsType)
	DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, CODE, UnderlyingMultiple)
	DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, CODE, CombinationType)
END_MDB_FIELD_NAME_TABLE(CTP, CODE)

//TICK
DEFINE_MDB_FIELD_COUNT(CTP, TICK);
DEFINE_MDB_FIELD(CTP, TICK, DATE, MDB_FIELD_TYPE_UINT32, sizeof(uint32_t));
DEFINE_MDB_FIELD(CTP, TICK, TIME, MDB_FIELD_TYPE_UINT32, sizeof(uint32_t));
DEFINE_MDB_FIELD(CTP, TICK, FLAGS, MDB_FIELD_TYPE_UINT32, sizeof(uint32_t));
DEFINE_MDB_FIELD(CTP, TICK, CLOSE, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
DEFINE_MDB_FIELD(CTP, TICK, AMOUNT, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
DEFINE_MDB_FIELD(CTP, TICK, VOLUME, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
DEFINE_MDB_FIELD(CTP, TICK, OpenInterest, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
BEGIN_MDB_FIELD_TABLE(CTP, TICK)
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, TICK, DATE)
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, TICK, TIME)
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, TICK, FLAGS)
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, TICK, CLOSE)
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, TICK, AMOUNT)
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, TICK, VOLUME)
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, TICK, OpenInterest)
END_MDB_FIELD_TABLE()

BEGIN_MDB_FIELD_NAME_TABLE(CTP, TICK)
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, TICK, DATE)
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, TICK, TIME)
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, TICK, FLAGS)
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, TICK, CLOSE)
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, TICK, AMOUNT)
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, TICK, VOLUME)
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, TICK, OpenInterest)
END_MDB_FIELD_NAME_TABLE(CTP, TICK)

//KDATA
DEFINE_MDB_FIELD_COUNT(CTP, KDATA);
DEFINE_MDB_FIELD(CTP, KDATA, DATE, MDB_FIELD_TYPE_UINT32, sizeof(uint32_t));
DEFINE_MDB_FIELD(CTP, KDATA, TIME, MDB_FIELD_TYPE_UINT32, sizeof(uint32_t));
DEFINE_MDB_FIELD(CTP, KDATA, OPEN, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
DEFINE_MDB_FIELD(CTP, KDATA, HIGH, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
DEFINE_MDB_FIELD(CTP, KDATA, LOW, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
DEFINE_MDB_FIELD(CTP, KDATA, CLOSE, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
DEFINE_MDB_FIELD(CTP, KDATA, AMOUNT, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
DEFINE_MDB_FIELD(CTP, KDATA, VOLUME, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
DEFINE_MDB_FIELD(CTP, KDATA, OpenInterest, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
BEGIN_MDB_FIELD_TABLE(CTP, KDATA)
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, KDATA, DATE)
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, KDATA, TIME)
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, KDATA, OPEN)
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, KDATA, HIGH)
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, KDATA, LOW)
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, KDATA, CLOSE)
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, KDATA, AMOUNT)
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, KDATA, VOLUME)
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, KDATA, OpenInterest)
END_MDB_FIELD_TABLE()

BEGIN_MDB_FIELD_NAME_TABLE(CTP, KDATA)
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, KDATA, DATE)
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, KDATA, TIME)
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, KDATA, OPEN)
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, KDATA, HIGH)
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, KDATA, LOW)
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, KDATA, CLOSE)
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, KDATA, AMOUNT)
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, KDATA, VOLUME)
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, KDATA, OpenInterest)
END_MDB_FIELD_NAME_TABLE(CTP, KDATA)

//USER
DEFINE_MDB_FIELD_COUNT(CTP, USER);
DEFINE_MDB_FIELD(CTP, USER, BROKER, MDB_FIELD_TYPE_STRING, MAX_CODE_LENGTH + 1);
DEFINE_MDB_FIELD(CTP, USER, USER, MDB_FIELD_TYPE_STRING, MAX_CODE_LENGTH + 1);
DEFINE_MDB_FIELD(CTP, USER, STATUS, MDB_FIELD_TYPE_CHAR, sizeof(char));
//
DEFINE_MDB_FIELD(CTP, USER, TradingDay, MDB_FIELD_TYPE_STRING, 9);
DEFINE_MDB_FIELD(CTP, USER, LoginTime, MDB_FIELD_TYPE_STRING, 9);
DEFINE_MDB_FIELD(CTP, USER, SystemName, MDB_FIELD_TYPE_STRING, 41);
DEFINE_MDB_FIELD(CTP, USER, FrontID, MDB_FIELD_TYPE_INT64, sizeof(int64_t));
DEFINE_MDB_FIELD(CTP, USER, SessionID, MDB_FIELD_TYPE_INT64, sizeof(int64_t));
DEFINE_MDB_FIELD(CTP, USER, MaxOrderRef, MDB_FIELD_TYPE_STRING, 13);
BEGIN_MDB_FIELD_TABLE(CTP, USER)
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, USER, BROKER)
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, USER, USER)
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, USER, STATUS)
//
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, USER, TradingDay)
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, USER, LoginTime)
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, USER, SystemName)
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, USER, FrontID)
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, USER, SessionID)
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, USER, MaxOrderRef)
END_MDB_FIELD_TABLE()

BEGIN_MDB_FIELD_NAME_TABLE(CTP, USER)
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, USER, BROKER)
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, USER, USER)
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, USER, STATUS)
//
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, USER, TradingDay)
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, USER, LoginTime)
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, USER, SystemName)
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, USER, FrontID)
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, USER, SessionID)
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, USER, MaxOrderRef)
END_MDB_FIELD_NAME_TABLE(CTP, USER)

//INVESTOR
DEFINE_MDB_FIELD_COUNT(CTP, INVESTOR);
DEFINE_MDB_FIELD(CTP, INVESTOR, BROKER, MDB_FIELD_TYPE_STRING, MAX_CODE_LENGTH + 1);
DEFINE_MDB_FIELD(CTP, INVESTOR, USER, MDB_FIELD_TYPE_STRING, MAX_CODE_LENGTH + 1);
DEFINE_MDB_FIELD(CTP, INVESTOR, INVESTOR, MDB_FIELD_TYPE_STRING, MAX_CODE_LENGTH + 1);
DEFINE_MDB_FIELD(CTP, INVESTOR, INVESTOR_NAME, MDB_FIELD_TYPE_STRING, MAX_NAME_LENGTH + 1);
//
DEFINE_MDB_FIELD(CTP, INVESTOR, InvestorGroupID, MDB_FIELD_TYPE_STRING, 13);
DEFINE_MDB_FIELD(CTP, INVESTOR, IdentifiedCardType, MDB_FIELD_TYPE_CHAR, sizeof(char));
DEFINE_MDB_FIELD(CTP, INVESTOR, IdentifiedCardNo, MDB_FIELD_TYPE_STRING, 51);
DEFINE_MDB_FIELD(CTP, INVESTOR, IsActive, MDB_FIELD_TYPE_BYTE, sizeof(uint8_t));
DEFINE_MDB_FIELD(CTP, INVESTOR, Telephone, MDB_FIELD_TYPE_STRING, 41);
DEFINE_MDB_FIELD(CTP, INVESTOR, Address, MDB_FIELD_TYPE_STRING, 101);
DEFINE_MDB_FIELD(CTP, INVESTOR, OpenDate, MDB_FIELD_TYPE_STRING, 9);
DEFINE_MDB_FIELD(CTP, INVESTOR, Mobile, MDB_FIELD_TYPE_STRING, 41);
DEFINE_MDB_FIELD(CTP, INVESTOR, CommModelID, MDB_FIELD_TYPE_STRING, 13);
DEFINE_MDB_FIELD(CTP, INVESTOR, MarginModelID, MDB_FIELD_TYPE_STRING, 13);
BEGIN_MDB_FIELD_TABLE(CTP, INVESTOR)
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, INVESTOR, BROKER)
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, INVESTOR, USER)
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, INVESTOR, INVESTOR)
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, INVESTOR, INVESTOR_NAME)
//
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, INVESTOR, InvestorGroupID)
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, INVESTOR, IdentifiedCardType)
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, INVESTOR, IdentifiedCardNo)
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, INVESTOR, IsActive)
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, INVESTOR, Telephone)
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, INVESTOR, Address)
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, INVESTOR, OpenDate)
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, INVESTOR, Mobile)
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, INVESTOR, CommModelID)
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, INVESTOR, MarginModelID)
END_MDB_FIELD_TABLE()

BEGIN_MDB_FIELD_NAME_TABLE(CTP, INVESTOR)
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, INVESTOR, BROKER)
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, INVESTOR, USER)
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, INVESTOR, INVESTOR)
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, INVESTOR, INVESTOR_NAME)
//
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, INVESTOR, InvestorGroupID)
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, INVESTOR, IdentifiedCardType)
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, INVESTOR, IdentifiedCardNo)
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, INVESTOR, IsActive)
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, INVESTOR, Telephone)
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, INVESTOR, Address)
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, INVESTOR, OpenDate)
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, INVESTOR, Mobile)
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, INVESTOR, CommModelID)
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, INVESTOR, MarginModelID)
END_MDB_FIELD_NAME_TABLE(CTP, INVESTOR)

//ACCOUNT
DEFINE_MDB_FIELD_COUNT(CTP, ACCOUNT);
DEFINE_MDB_FIELD(CTP, ACCOUNT, BROKER, MDB_FIELD_TYPE_STRING, MAX_CODE_LENGTH + 1);
DEFINE_MDB_FIELD(CTP, ACCOUNT, USER, MDB_FIELD_TYPE_STRING, MAX_CODE_LENGTH + 1);
DEFINE_MDB_FIELD(CTP, ACCOUNT, ACCOUNT, MDB_FIELD_TYPE_STRING, MAX_CODE_LENGTH + 1);
//
DEFINE_MDB_FIELD(CTP, ACCOUNT, InvestorGroupID, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
DEFINE_MDB_FIELD(CTP, ACCOUNT, PreMortgage, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
DEFINE_MDB_FIELD(CTP, ACCOUNT, PreCredit, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
DEFINE_MDB_FIELD(CTP, ACCOUNT, PreDeposit, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
DEFINE_MDB_FIELD(CTP, ACCOUNT, PreBalance, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
DEFINE_MDB_FIELD(CTP, ACCOUNT, PreMargin, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
DEFINE_MDB_FIELD(CTP, ACCOUNT, InterestBase, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
DEFINE_MDB_FIELD(CTP, ACCOUNT, Interest, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
DEFINE_MDB_FIELD(CTP, ACCOUNT, Deposit, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
DEFINE_MDB_FIELD(CTP, ACCOUNT, Withdraw, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
DEFINE_MDB_FIELD(CTP, ACCOUNT, FrozenMargin, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
DEFINE_MDB_FIELD(CTP, ACCOUNT, FrozenCash, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
DEFINE_MDB_FIELD(CTP, ACCOUNT, FrozenCommission, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
DEFINE_MDB_FIELD(CTP, ACCOUNT, CurrMargin, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
DEFINE_MDB_FIELD(CTP, ACCOUNT, CashIn, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
DEFINE_MDB_FIELD(CTP, ACCOUNT, Commission, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
DEFINE_MDB_FIELD(CTP, ACCOUNT, CloseProfit, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
DEFINE_MDB_FIELD(CTP, ACCOUNT, PositionProfit, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
DEFINE_MDB_FIELD(CTP, ACCOUNT, Balance, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
DEFINE_MDB_FIELD(CTP, ACCOUNT, Available, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
DEFINE_MDB_FIELD(CTP, ACCOUNT, WithdrawQuota, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
DEFINE_MDB_FIELD(CTP, ACCOUNT, Reserve, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
DEFINE_MDB_FIELD(CTP, ACCOUNT, TradingDay, MDB_FIELD_TYPE_STRING, 9);
DEFINE_MDB_FIELD(CTP, ACCOUNT, SettlementID, MDB_FIELD_TYPE_UINT64, sizeof(uint64_t));
DEFINE_MDB_FIELD(CTP, ACCOUNT, Credit, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
DEFINE_MDB_FIELD(CTP, ACCOUNT, Mortgage, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
DEFINE_MDB_FIELD(CTP, ACCOUNT, ExchangeMargin, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
DEFINE_MDB_FIELD(CTP, ACCOUNT, DeliveryMargin, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
DEFINE_MDB_FIELD(CTP, ACCOUNT, ExchangeDeliveryMargin, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
DEFINE_MDB_FIELD(CTP, ACCOUNT, ReserveBalance, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
DEFINE_MDB_FIELD(CTP, ACCOUNT, CurrencyID, MDB_FIELD_TYPE_STRING, 4);
DEFINE_MDB_FIELD(CTP, ACCOUNT, PreFundMortgageIn, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
DEFINE_MDB_FIELD(CTP, ACCOUNT, PreFundMortgageOut, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
DEFINE_MDB_FIELD(CTP, ACCOUNT, FundMortgageAvailable, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
DEFINE_MDB_FIELD(CTP, ACCOUNT, MortgageableFund, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
DEFINE_MDB_FIELD(CTP, ACCOUNT, SpecProductMargin, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
DEFINE_MDB_FIELD(CTP, ACCOUNT, SpecProductFrozenMargin, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
DEFINE_MDB_FIELD(CTP, ACCOUNT, SpecProductCommission, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
DEFINE_MDB_FIELD(CTP, ACCOUNT, SpecProductPositionProfit, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
DEFINE_MDB_FIELD(CTP, ACCOUNT, SpecProductCloseProfit, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
DEFINE_MDB_FIELD(CTP, ACCOUNT, SpecProductPositionProfitByAlg, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
DEFINE_MDB_FIELD(CTP, ACCOUNT, SpecProductExchangeMargin, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
DEFINE_MDB_FIELD(CTP, ACCOUNT, BizType, MDB_FIELD_TYPE_CHAR, sizeof(char));
DEFINE_MDB_FIELD(CTP, ACCOUNT, FrozenSwap, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
DEFINE_MDB_FIELD(CTP, ACCOUNT, RemainSwap, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
BEGIN_MDB_FIELD_TABLE(CTP, ACCOUNT)
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, ACCOUNT, BROKER)
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, ACCOUNT, USER)
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, ACCOUNT, ACCOUNT)
//
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, ACCOUNT, InvestorGroupID)
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, ACCOUNT, PreMortgage)
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, ACCOUNT, PreCredit)
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, ACCOUNT, PreDeposit)
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, ACCOUNT, PreBalance)
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, ACCOUNT, PreMargin)
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, ACCOUNT, InterestBase)
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, ACCOUNT, Interest)
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, ACCOUNT, Deposit)
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, ACCOUNT, Withdraw)
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, ACCOUNT, FrozenMargin)
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, ACCOUNT, FrozenCash)
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, ACCOUNT, FrozenCommission)
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, ACCOUNT, CurrMargin)
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, ACCOUNT, CashIn)
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, ACCOUNT, Commission)
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, ACCOUNT, CloseProfit)
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, ACCOUNT, PositionProfit)
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, ACCOUNT, Balance)
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, ACCOUNT, Available)
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, ACCOUNT, WithdrawQuota)
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, ACCOUNT, Reserve)
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, ACCOUNT, TradingDay)
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, ACCOUNT, SettlementID)
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, ACCOUNT, Credit)
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, ACCOUNT, Mortgage)
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, ACCOUNT, ExchangeMargin)
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, ACCOUNT, DeliveryMargin)
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, ACCOUNT, ExchangeDeliveryMargin)
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, ACCOUNT, ReserveBalance)
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, ACCOUNT, CurrencyID)
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, ACCOUNT, PreFundMortgageIn)
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, ACCOUNT, PreFundMortgageOut)
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, ACCOUNT, FundMortgageAvailable)
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, ACCOUNT, MortgageableFund)
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, ACCOUNT, SpecProductMargin)
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, ACCOUNT, SpecProductFrozenMargin)
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, ACCOUNT, SpecProductCommission)
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, ACCOUNT, SpecProductPositionProfit)
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, ACCOUNT, SpecProductCloseProfit)
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, ACCOUNT, SpecProductPositionProfitByAlg)
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, ACCOUNT, SpecProductExchangeMargin)
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, ACCOUNT, BizType)
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, ACCOUNT, FrozenSwap)
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, ACCOUNT, RemainSwap)
END_MDB_FIELD_TABLE()

BEGIN_MDB_FIELD_NAME_TABLE(CTP, ACCOUNT)
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, ACCOUNT, BROKER)
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, ACCOUNT, USER)
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, ACCOUNT, ACCOUNT)
//
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, ACCOUNT, InvestorGroupID)
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, ACCOUNT, PreMortgage)
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, ACCOUNT, PreCredit)
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, ACCOUNT, PreDeposit)
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, ACCOUNT, PreBalance)
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, ACCOUNT, PreMargin)
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, ACCOUNT, InterestBase)
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, ACCOUNT, Interest)
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, ACCOUNT, Deposit)
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, ACCOUNT, Withdraw)
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, ACCOUNT, FrozenMargin)
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, ACCOUNT, FrozenCash)
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, ACCOUNT, FrozenCommission)
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, ACCOUNT, CurrMargin)
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, ACCOUNT, CashIn)
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, ACCOUNT, Commission)
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, ACCOUNT, CloseProfit)
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, ACCOUNT, PositionProfit)
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, ACCOUNT, Balance)
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, ACCOUNT, Available)
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, ACCOUNT, WithdrawQuota)
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, ACCOUNT, Reserve)
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, ACCOUNT, TradingDay)
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, ACCOUNT, SettlementID)
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, ACCOUNT, Credit)
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, ACCOUNT, Mortgage)
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, ACCOUNT, ExchangeMargin)
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, ACCOUNT, DeliveryMargin)
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, ACCOUNT, ExchangeDeliveryMargin)
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, ACCOUNT, ReserveBalance)
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, ACCOUNT, CurrencyID)
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, ACCOUNT, PreFundMortgageIn)
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, ACCOUNT, PreFundMortgageOut)
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, ACCOUNT, FundMortgageAvailable)
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, ACCOUNT, MortgageableFund)
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, ACCOUNT, SpecProductMargin)
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, ACCOUNT, SpecProductFrozenMargin)
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, ACCOUNT, SpecProductCommission)
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, ACCOUNT, SpecProductPositionProfit)
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, ACCOUNT, SpecProductCloseProfit)
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, ACCOUNT, SpecProductPositionProfitByAlg)
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, ACCOUNT, SpecProductExchangeMargin)
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, ACCOUNT, BizType)
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, ACCOUNT, FrozenSwap)
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, ACCOUNT, RemainSwap)
END_MDB_FIELD_NAME_TABLE(CTP, ACCOUNT)

//COMMISSIONRATE
DEFINE_MDB_FIELD_COUNT(CTP, COMMISSIONRATE);
DEFINE_MDB_FIELD(CTP, COMMISSIONRATE, BROKER, MDB_FIELD_TYPE_STRING, MAX_CODE_LENGTH + 1);
DEFINE_MDB_FIELD(CTP, COMMISSIONRATE, USER, MDB_FIELD_TYPE_STRING, MAX_CODE_LENGTH + 1);
DEFINE_MDB_FIELD(CTP, COMMISSIONRATE, INVESTOR, MDB_FIELD_TYPE_STRING, MAX_CODE_LENGTH + 1);
//
DEFINE_MDB_FIELD(CTP, COMMISSIONRATE, InstrumentID, MDB_FIELD_TYPE_STRING, 31);
DEFINE_MDB_FIELD(CTP, COMMISSIONRATE, InvestorRange, MDB_FIELD_TYPE_CHAR, sizeof(char));
DEFINE_MDB_FIELD(CTP, COMMISSIONRATE, OpenRatioByMoney, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
DEFINE_MDB_FIELD(CTP, COMMISSIONRATE, OpenRatioByVolume, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
DEFINE_MDB_FIELD(CTP, COMMISSIONRATE, CloseRatioByMoney, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
DEFINE_MDB_FIELD(CTP, COMMISSIONRATE, CloseRatioByVolume, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
DEFINE_MDB_FIELD(CTP, COMMISSIONRATE, CloseTodayRatioByMoney, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
DEFINE_MDB_FIELD(CTP, COMMISSIONRATE, CloseTodayRatioByVolume, MDB_FIELD_TYPE_DOUBLE, sizeof(double));
DEFINE_MDB_FIELD(CTP, COMMISSIONRATE, ExchangeID, MDB_FIELD_TYPE_STRING, 9);
DEFINE_MDB_FIELD(CTP, COMMISSIONRATE, BizType, MDB_FIELD_TYPE_CHAR, sizeof(char));
DEFINE_MDB_FIELD(CTP, COMMISSIONRATE, InvestUnitID, MDB_FIELD_TYPE_STRING, 17);
BEGIN_MDB_FIELD_TABLE(CTP, COMMISSIONRATE)
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, COMMISSIONRATE, BROKER)
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, COMMISSIONRATE, USER)
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, COMMISSIONRATE, INVESTOR)
//
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, COMMISSIONRATE, InstrumentID)
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, COMMISSIONRATE, InvestorRange)
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, COMMISSIONRATE, OpenRatioByMoney)
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, COMMISSIONRATE, OpenRatioByVolume)
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, COMMISSIONRATE, CloseRatioByMoney)
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, COMMISSIONRATE, CloseRatioByVolume)
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, COMMISSIONRATE, CloseTodayRatioByMoney)
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, COMMISSIONRATE, CloseTodayRatioByVolume)
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, COMMISSIONRATE, ExchangeID)
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, COMMISSIONRATE, BizType)
DEFINE_MDB_FIELD_TABLE_FIELD(CTP, COMMISSIONRATE, InvestUnitID)
END_MDB_FIELD_TABLE()

BEGIN_MDB_FIELD_NAME_TABLE(CTP, COMMISSIONRATE)
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, COMMISSIONRATE, BROKER)
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, COMMISSIONRATE, USER)
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, COMMISSIONRATE, INVESTOR)
//
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, COMMISSIONRATE, InstrumentID)
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, COMMISSIONRATE, InvestorRange)
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, COMMISSIONRATE, OpenRatioByMoney)
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, COMMISSIONRATE, OpenRatioByVolume)
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, COMMISSIONRATE, CloseRatioByMoney)
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, COMMISSIONRATE, CloseRatioByVolume)
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, COMMISSIONRATE, CloseTodayRatioByMoney)
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, COMMISSIONRATE, CloseTodayRatioByVolume)
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, COMMISSIONRATE, ExchangeID)
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, COMMISSIONRATE, BizType)
DEFINE_MDB_FIELD_NAME_TABLE_FIELD(CTP, COMMISSIONRATE, InvestUnitID)
END_MDB_FIELD_NAME_TABLE(CTP, COMMISSIONRATE)

// utf8转gbk
std::string utf8_to_gbk(const std::string &str)
{
	std::wstring_convert<std::codecvt_utf8<wchar_t>> utf8_cvt; // utf8 -> unicode
	std::wstring_convert<std::codecvt<wchar_t, char, std::mbstate_t>> gbk_cvt(new std::codecvt<wchar_t, char, std::mbstate_t>("chs")); // unicode -> gbk
	std::wstring t = utf8_cvt.from_bytes(str);
	return gbk_cvt.to_bytes(t);
}

// gbk转utf8
std::string gbk_to_utf8(const std::string &str)
{
	std::wstring_convert<std::codecvt_utf8<wchar_t>> utf8_cvt; // utf8 -> unicode
	std::wstring_convert<std::codecvt<wchar_t, char, std::mbstate_t>> gbk_cvt(new std::codecvt<wchar_t, char, std::mbstate_t>("chs")); // unicode -> gbk
	std::wstring t = gbk_cvt.from_bytes(str);
	return utf8_cvt.to_bytes(t);
}


#define CTP_API_REQ_LOGIN	"ReqLogin"

bool parseUserInfoFromString(CTPUserLoginInfo& userInfo, const std::string& jsonString)
{
	try
	{
		boost::property_tree::ptree jsonObject;
		if (XUtil::json_from_str(jsonString, jsonObject)) {
			///RequestApi
			std::string RequestApi = jsonObject.get<std::string>("RequestApi");
			if (RequestApi != CTP_API_REQ_LOGIN) {
				return false;
			}
			//获取Data
			const boost::property_tree::ptree & jsonData = jsonObject.get_child("Data");
			///经纪公司代码
			snprintf(userInfo.BrokerID, _countof(userInfo.BrokerID), "%s", jsonData.get<std::string>("BrokerID","").c_str());
			///用户代码
			snprintf(userInfo.UserID, _countof(userInfo.UserID), "%s", jsonData.get<std::string>("UserID","").c_str());
			///密码
			snprintf(userInfo.Password, _countof(userInfo.Password), "%s", jsonData.get<std::string>("Password","").c_str());
			///用户端产品信息
			snprintf(userInfo.UserProductInfo, _countof(userInfo.UserProductInfo), "%s", jsonData.get<std::string>("UserProductInfo", "").c_str());
			///接口端产品信息
			snprintf(userInfo.InterfaceProductInfo, _countof(userInfo.InterfaceProductInfo), "%s", jsonData.get<std::string>("InterfaceProductInfo", "").c_str());
			///协议信息
			snprintf(userInfo.ProtocolInfo, _countof(userInfo.ProtocolInfo), "%s", jsonData.get<std::string>("ProtocolInfo", "").c_str());
			///Mac地址
			snprintf(userInfo.MacAddress, _countof(userInfo.MacAddress), "%s", jsonData.get<std::string>("MacAddress", "").c_str());
			///动态密码
			snprintf(userInfo.OneTimePassword, _countof(userInfo.OneTimePassword), "%s", jsonData.get<std::string>("OneTimePassword", "").c_str());
			///终端IP地址
			snprintf(userInfo.ClientIPAddress, _countof(userInfo.ClientIPAddress), "%s", jsonData.get<std::string>("ClientIPAddress", "").c_str());
			///登录备注
			snprintf(userInfo.LoginRemark, _countof(userInfo.LoginRemark), "%s", jsonData.get<std::string>("LoginRemark", "").c_str());
			///前置机服务器网络地址
			auto opt_front = jsonData.get_child_optional("FrontAddress");
			if (opt_front) {
				BOOST_FOREACH(const boost::property_tree::ptree::value_type &cfg_front, opt_front.get())
				{
					auto addr = cfg_front.second.get_value<std::string>();
					if (addr.find("://") != addr.npos) {
						userInfo.FrontAddress.push_back(addr);
					}
					else {
						userInfo.FrontAddress.push_back("tcp://" + addr);
					}
				}
			}
			///名字服务器网络地址
			auto opt_nsaddr = jsonData.get_child_optional("NsAddress");
			if (opt_nsaddr) {
				BOOST_FOREACH(const boost::property_tree::ptree::value_type &cfg_addr, opt_nsaddr.get())
				{
					auto addr = cfg_addr.second.get_value<std::string>();
					if (addr.find("://") != addr.npos) {
						userInfo.NsAddress.push_back(addr);
					}
					else {
						userInfo.NsAddress.push_back("tcp://" + addr);
					}
				}
			}
			///登录模式
			userInfo.LoginMode = jsonData.get<TThostFtdcLoginModeType>("LoginMode", (char)0);
			///认证码
			snprintf(userInfo.AuthCode, _countof(userInfo.AuthCode), "%s", jsonData.get<std::string>("AuthCode", "").c_str());
			///行情前置机服务器网络地址
			auto opt_md_front = jsonData.get_child_optional("MdFrontAddress");
			if (opt_md_front) {
				BOOST_FOREACH(const boost::property_tree::ptree::value_type &cfg_md_front, opt_md_front.get())
				{
					auto addr = cfg_md_front.second.get_value<std::string>();
					if (addr.find("://") != addr.npos) {
						userInfo.MdFrontAddress.push_back(addr);
					}
					else {
						userInfo.MdFrontAddress.push_back("tcp://" + addr);
					}
				}
			}
			///行情名字服务器网络地址
			auto opt_md_nsaddr = jsonData.get_child_optional("MdNsAddress");
			if (opt_nsaddr) {
				BOOST_FOREACH(const boost::property_tree::ptree::value_type &cfg_md_addr, opt_md_nsaddr.get())
				{
					auto addr = cfg_md_addr.second.get_value<std::string>();
					if (addr.find("://") != addr.npos) {
						userInfo.MdNsAddress.push_back(addr);
					}
					else {
						userInfo.MdNsAddress.push_back("tcp://" + addr);
					}
				}
			}
			if (userInfo.BrokerID[0] && userInfo.UserID[0] && userInfo.Password[0] && !userInfo.FrontAddress.empty())
			{
				return true;
			}
		}
	}
	catch (...)
	{
	}
	return false;
}

bool parse_userinfo(CTPUserLoginInfo& userinfo, const boost::property_tree::ptree& cfg, bool market)
{
	try
	{
		///经纪公司代码
		snprintf(userinfo.BrokerID, _countof(userinfo.BrokerID), "%s", cfg.get<std::string>("BrokerID", "").c_str());
		///用户代码
		snprintf(userinfo.UserID, _countof(userinfo.UserID), "%s", cfg.get<std::string>("UserID", "").c_str());
		///密码
		snprintf(userinfo.Password, _countof(userinfo.Password), "%s", cfg.get<std::string>("Password", "").c_str());
		///用户端产品信息
		snprintf(userinfo.UserProductInfo, _countof(userinfo.UserProductInfo), "%s", cfg.get<std::string>("UserProductInfo", "").c_str());
		///接口端产品信息
		snprintf(userinfo.InterfaceProductInfo, _countof(userinfo.InterfaceProductInfo), "%s", cfg.get<std::string>("InterfaceProductInfo", "").c_str());
		///协议信息
		snprintf(userinfo.ProtocolInfo, _countof(userinfo.ProtocolInfo), "%s", cfg.get<std::string>("ProtocolInfo", "").c_str());
		///Mac地址
		snprintf(userinfo.MacAddress, _countof(userinfo.MacAddress), "%s", cfg.get<std::string>("MacAddress", "").c_str());
		///动态密码
		snprintf(userinfo.OneTimePassword, _countof(userinfo.OneTimePassword), "%s", cfg.get<std::string>("OneTimePassword", "").c_str());
		///终端IP地址
		snprintf(userinfo.ClientIPAddress, _countof(userinfo.ClientIPAddress), "%s", cfg.get<std::string>("ClientIPAddress", "").c_str());
		///登录备注
		snprintf(userinfo.LoginRemark, _countof(userinfo.LoginRemark), "%s", cfg.get<std::string>("LoginRemark", "").c_str());
		///前置机服务器网络地址
		auto opt_front = cfg.get_child_optional("FrontAddress");
		if (opt_front) {
			BOOST_FOREACH(const boost::property_tree::ptree::value_type &cfg_front, opt_front.get())
			{
				auto addr = cfg_front.second.get_value<std::string>(); 
				if (addr.find("://") != addr.npos) {
					userinfo.FrontAddress.push_back(addr);
				}
				else {
					userinfo.FrontAddress.push_back("tcp://" + addr);
				}
			}
		}
		///名字服务器网络地址
		auto opt_nsaddr = cfg.get_child_optional("NsAddress");
		if (opt_nsaddr) {
			BOOST_FOREACH(const boost::property_tree::ptree::value_type &cfg_addr, opt_nsaddr.get())
			{
				auto addr = cfg_addr.second.get_value<std::string>();
				if (addr.find("://") != addr.npos) {
					userinfo.NsAddress.push_back(addr);
				}
				else {
					userinfo.NsAddress.push_back("tcp://" + addr);
				}
			}
		}
		///登录模式
		userinfo.LoginMode = cfg.get<TThostFtdcLoginModeType>("LoginMode", (char)0);
		///认证码
		snprintf(userinfo.AuthCode, _countof(userinfo.AuthCode), "%s", cfg.get<std::string>("AuthCode", "").c_str());
		///App代码
		snprintf(userinfo.AppID, _countof(userinfo.AppID), "%s", cfg.get<std::string>("AppID", "").c_str());
		///行情前置机服务器网络地址
		auto opt_md_front = cfg.get_child_optional("MdFrontAddress");
		if (opt_md_front)
		{
			BOOST_FOREACH(const boost::property_tree::ptree::value_type &cfg_md_front, opt_md_front.get())
			{
				auto addr = cfg_md_front.second.get_value<std::string>();
				if (addr.find("://") != addr.npos) {
					userinfo.MdFrontAddress.push_back(addr);
				}
				else {
					userinfo.MdFrontAddress.push_back("tcp://" + addr);
				}
			}
		}
		///行情名字服务器网络地址
		auto opt_md_nsaddr = cfg.get_child_optional("MdNsAddress");
		if (opt_nsaddr)
		{
			BOOST_FOREACH(const boost::property_tree::ptree::value_type &cfg_md_addr, opt_md_nsaddr.get())
			{
				auto addr = cfg_md_addr.second.get_value<std::string>();
				if (addr.find("://") != addr.npos) {
					userinfo.MdNsAddress.push_back(addr);
				}
				else {
					userinfo.MdNsAddress.push_back("tcp://" + addr);
				}
			}
		}
		if (market) {
			if (!userinfo.MdFrontAddress.empty()) {
				return true;
			}
		}
		else {
			if (userinfo.BrokerID[0] && userinfo.UserID[0] && userinfo.Password[0] && !userinfo.FrontAddress.empty()) {
				return true;
			}
		}
	}
	catch (...)
	{
	}
	return false;
}

//////////////////////////////////////////////////////////////////////////

// 是否收到成功的响应
bool IsErrorRspInfo(const CThostFtdcRspInfoField *pRspInfo)
{
	if (pRspInfo)
	{
		// 如果ErrorID != 0, 说明收到了错误的响应
		if (pRspInfo->ErrorID != 0)
		{
			PRINTF("%s\n", pRspInfo->ErrorMsg);
			return true;
		}
	}
	return false;
}

//////////////////////////////////////////////////////////////////////////

unsigned long CTPDate2Date(const TThostFtdcDateType& Date)
{
	//20180302
	return XUtil::strto<unsigned long>(Date);
}

unsigned long CTPTime2Time(const TThostFtdcTimeType& Time)
{
	//13:12:12 会出现 --:--:--
	unsigned long HH = 0, MM = 0, SS = 0;
	HH = XUtil::strto<unsigned long>(&Time[0]);
	if (!(HH >= 0 && HH <= 23)) {
		return 0;
	}
	MM = XUtil::strto<unsigned long>(&Time[3]);
	if (!(MM >= 0 && MM <= 59)) {
		return 0;
	}
	SS = XUtil::strto<unsigned long>(&Time[6]);
	if (!(SS >= 0 && SS <= 60)) {
		return 0;
	}
	return XUtil::MakeTime(HH, MM, SS);
}

///

//比较更新判断，判断是更新旧周期还是新增周期
bool SameCycle(HZQDB h, uint32_t olddate, uint32_t newdate, uint32_t oldtime, uint32_t newtime, PERIODTYPE cycle, size_t cycleex)
{
	if (cycle < CYC_DAY) {
		return olddate == newdate && oldtime == newtime;
	}
	else if (cycle == CYC_DAY) {
		return olddate == newdate;
	}
	return ZQDBSameCycle(olddate, newdate, oldtime, newtime, cycle, cycleex);
}

///

static CTPGlobal g_global;

CTPGlobal::CTPGlobal()
{
	boost::filesystem::path::imbue(
		std::locale(std::locale(), new std::codecvt_utf8_utf16<wchar_t>()));
	
	//表结构要在这之前定义好
	MDBFieldNormalize(MDB_FIELD_TABLE(CTP, CODE), MDB_FIELD_TABLE_FIELD_COUNT(CTP, CODE));
	MDBFieldNormalize(MDB_FIELD_TABLE(CTP, TICK), MDB_FIELD_TABLE_FIELD_COUNT(CTP, TICK));
	MDBFieldNormalize(MDB_FIELD_TABLE(CTP, KDATA), MDB_FIELD_TABLE_FIELD_COUNT(CTP, KDATA));
	MDBFieldNormalize(MDB_FIELD_TABLE(CTP, USER), MDB_FIELD_TABLE_FIELD_COUNT(CTP, USER));
	MDBFieldNormalize(MDB_FIELD_TABLE(CTP, INVESTOR), MDB_FIELD_TABLE_FIELD_COUNT(CTP, INVESTOR));
	MDBFieldNormalize(MDB_FIELD_TABLE(CTP, ACCOUNT), MDB_FIELD_TABLE_FIELD_COUNT(CTP, ACCOUNT));
	//MDBFieldNormalize(PositionInfo_Field, PositionInfo_Field_Num);
	//MDBFieldNormalize(OrderInfo_Field, OrderInfo_Field_Num);
	//MDBFieldNormalize(TradeInfo_Field, TradeInfo_Field_Num);
}